﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Web;
using System.Web.UI.WebControls;

namespace Portal.CMS
{
    public class UploadImage : WebPart
    {
      
        public override string WebPartId
        {
            get
            {
                return "UploadImage";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Hình đại diện tin tức");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        System.Web.UI.WebControls.Button SaveButton = null;
        System.Web.UI.WebControls.Button SaveButtonLargeOnly = null;
        System.Web.UI.WebControls.Button SaveButtonSmallOnly = null;
        System.Web.UI.WebControls.Label labelError = null;
        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl, System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UploadImage));

            labelError = new System.Web.UI.WebControls.Label();
            labelError.CssClass = "ErrorUploadNewsImage";
            SaveButton = new System.Web.UI.WebControls.Button();
            SaveButton.Text = ResourceBLL.GetText(WebPartId, "UploadAll", "Upload cả hai");
            SaveButton.Click += new EventHandler(SaveButton_Click);

            SaveButtonLargeOnly = new Button();
            SaveButtonLargeOnly.Text = ResourceBLL.GetText(WebPartId, "UploadLarge", "Upload ảnh lớn");
            SaveButtonLargeOnly.Click += new EventHandler(SaveButtonLargeOnly_Click);

            SaveButtonSmallOnly = new Button();
            SaveButtonSmallOnly.Text = ResourceBLL.GetText(WebPartId, "UploadSmall", "Upload ảnh nhỏ");
            SaveButtonSmallOnly.Click += new EventHandler(SaveButtonSmallOnly_Click);

            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetHeaderFormDialog(ResourceBLL.GetText(WebPartId,"Title","Cập nhật ảnh cho bản tin!"))));
            pl.Controls.Add(labelError);
            pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId, "Help", "<br><br><span class=cssHelpUploadImageForNews>Chọn file ảnh cần upload hoặc để trắng để xóa ảnh<br>Nếu chỉ upload ảnh nhỏ thì kích vào nút ảnh nhỏ<br>Ảnh lớn kích vào nút upload ảnh lớn<br>Nút upload ở dưới để upload cả 2 ảnh nhỏ và lớn!</span>")));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId,"FileName","Upload file:")));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input name=\"txtItemName\" id=\"txtItemName\" type=\"file\" runat=server>"));
            pl.Controls.Add(SaveButtonLargeOnly);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(ResourceBLL.GetText(WebPartId, "FileNameSmall", "Small file:")));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input name=\"txtItemNameSmall\" id=\"txtItemNameSmall\" type=\"file\" runat=server>"));
            pl.Controls.Add(SaveButtonSmallOnly);
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td></td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("<td>"));
            pl.Controls.Add(SaveButton);
            pl.Controls.Add(new System.Web.UI.LiteralControl("<input type=button onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Thoát") + "\">"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</td>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</tr>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl("</table>"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(Environments.GetFooterFormDialog()));
        }

        void SaveButtonSmallOnly_Click(object sender, EventArgs e)
        {
            try
            {
                string NewsId = (string)System.Web.HttpContext.Current.Request["NewsId"];
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);

                labelError.Text = "";
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;


                string WorkingPath = Environments.WorkingPath;

                HttpPostedFile hf = (HttpPostedFile)files[1];
                string PathSave = WorkingPath + "\\Media";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);


                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images\\" + NewsCls.belongSite;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                string FileSave = PathSave + "\\" + hf.FileName;
                string UploadName = "";
                if (hf.FileName.Length > 0)
                {
                    hf.SaveAs(FileSave);
                    byte[] b = Environments.GetBytesFromFile(FileSave);
                    Environments.UploadFileWithWs(FileSave, b, WorkingPath);

                    UploadName = new System.IO.FileInfo(FileSave).Name;
                }

                ServerSideUploadSmall(NewsCls.belongSite, NewsId, FileSave);
                labelError.Text = ResourceBLL.GetText(WebPartId, "UploadSmallOk", "Upload ảnh minh họa nhỏ thành công!");
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message.ToString();
            }
        }

        void SaveButtonLargeOnly_Click(object sender, EventArgs e)
        {
            try
            {
                string NewsId = (string)System.Web.HttpContext.Current.Request["NewsId"];
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);

                labelError.Text = "";
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

                string WorkingPath = Environments.WorkingPath;
                HttpPostedFile hf = (HttpPostedFile)files[0];
                string PathSave = WorkingPath + "\\Media";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images\\" + NewsCls.belongSite;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                string FileSave = PathSave + "\\" + hf.FileName;
                string UploadName = "";
                if (hf.FileName.Length > 0)
                {
                    hf.SaveAs(FileSave);
                    byte[] b = Environments.GetBytesFromFile(FileSave);
                    Environments.UploadFileWithWs(FileSave, b, WorkingPath);
                    UploadName = new System.IO.FileInfo(FileSave).Name;
                }

                ServerSideUploadLarge(NewsCls.belongSite, NewsId, FileSave);
                labelError.Text = ResourceBLL.GetText(WebPartId, "UploadLargeOk", "Upload ảnh minh họa lớn thành công!");
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message.ToString();
            }
        }

        void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                string NewsId = (string)System.Web.HttpContext.Current.Request["NewsId"];
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);

                labelError.Text = "";
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

                string WorkingPath = Environments.WorkingPath;
                string PathSave = WorkingPath + "\\Media";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images";
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);

                PathSave = WorkingPath + "\\Media\\" + Environments.PortalAlias + "\\News\\Images\\" + NewsCls.belongSite;
                System.IO.Directory.CreateDirectory(PathSave);
                Environments.CreateDirectoryWithWs(PathSave, WorkingPath);


               
                HttpPostedFile hf = (HttpPostedFile)files[0];
                string FileSave = PathSave + "\\" + hf.FileName;
                string UploadName = "";
                if (hf.FileName.Length > 0)
                {
                    hf.SaveAs(FileSave);
                    byte[] b = Environments.GetBytesFromFile(FileSave);
                    Environments.UploadFileWithWs(FileSave, b, WorkingPath);

                    UploadName = new System.IO.FileInfo(FileSave).Name;
                }
                string largeFile = FileSave;
                

                hf = (HttpPostedFile)files[1];
                
                FileSave = PathSave + "\\" + hf.FileName;
                if (hf.FileName.Trim().Length > 0)
                {
                    hf.SaveAs(FileSave);
                    byte[] b = Environments.GetBytesFromFile(FileSave);
                    Environments.UploadFileWithWs(FileSave, b, WorkingPath);

                }
                UploadName = new System.IO.FileInfo(FileSave).Name;
                string smallFile = FileSave;
                ServerSideUpload(NewsCls.belongSite, NewsId, largeFile, smallFile);
                labelError.Text = ResourceBLL.GetText(WebPartId,"UploadOk","Upload ảnh minh họa thành công!");
            }
            catch (Exception ex)
            {
                labelError.Text = ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpload(string belongSite,string NewsId, string largeFile, string smallFile)
        {
            new NewsBLL().UpdateImage(null, NewsId, belongSite, largeFile, smallFile);
            return ResourceBLL.GetText(WebPartId,"MessageUploadOk","Upload thành công!");
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideUploadLarge(string belongSite, string NewsId, string UploadFile)
        {
            new NewsBLL().UpdateLargeImage(null, NewsId, belongSite,UploadFile);
            return ResourceBLL.GetText(WebPartId, "MessageUploadOk", "Upload thành công!");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUploadSmall(string belongSite,string NewsId, string UploadFile)
        {
            new NewsBLL().UpdateSmallImage(null,  NewsId,belongSite, UploadFile);
            return ResourceBLL.GetText(WebPartId, "MessageUploadOk", "Upload thành công!");
        }
    }
}